/**
 * This class is responsible for Wizard panels transitions
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JRadioButton;

import org.openswing.swing.wizard.client.WizardController;
import org.openswing.swing.wizard.client.WizardPanel;

/**
 * PivotTableWizardController.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotTableWizardController extends WizardController implements ItemListener {

    private WizardPanel wizardPanel = null;
    private boolean conditionalScreen = true;

    public PivotTableWizardController(WizardPanel wizardPanel) {
	this.wizardPanel = wizardPanel;
    }

    /**
     * Method called by WizardPanel when showing the panel the first time
     * 
     * @param wizard
     *            wizard panel
     * @return panel identifier of the panel to show
     */
    @Override
    public String getFirstPanelId(WizardPanel wizard) {
	return PivotWizardFirstPanel.SCREEN_ID;
    }

    /**
     * Method called by WizardPanel to check which panel is the last one
     * 
     * @param wizard
     *            wizard panel
     * @return panel identifier of the last panel
     */
    @Override
    public String getLastPanelId(WizardPanel wizard) {
	return PivotWizardPostAnalysisPivotPanel.SCREEN_ID;
    }

    /**
     * Method called by WizardPanel when when pressing "Back" button
     * 
     * @param wizard
     *            wizard panel
     * @return panel identifier of the panel to show
     */
    @Override
    public String getBackPanelId(WizardPanel wizard) {
	String currentVisiblePanelId = wizard.getCurrentVisiblePanel().getPanelId();
	if (currentVisiblePanelId.equals(PivotWizardSecondPanel.SCREEN_ID)
		|| currentVisiblePanelId.equals(PivotWizardFirstPanel.SCREEN_ID))
	    return PivotWizardFirstPanel.SCREEN_ID;
	if (currentVisiblePanelId.equals(PivotWizardAnalysisPanel.SCREEN_ID))
	    return PivotWizardSecondPanel.SCREEN_ID;
	else
	    return null;
    }

    /**
     * Method called by WizardPanel when pressing "Next" button
     * 
     * @param wizard
     *            wizard panel
     * @return panel identifier of the panel to show
     */
    @Override
    public String getNextPanelId(WizardPanel wizard) {
	String currentPanelId = wizard.getCurrentVisiblePanel().getPanelId();

	if (currentPanelId.equals(PivotWizardFirstPanel.SCREEN_ID))
	    return PivotWizardSecondPanel.SCREEN_ID;
	if (currentPanelId.equals(PivotWizardSecondPanel.SCREEN_ID)) {
	    return PivotWizardAnalysisPanel.SCREEN_ID;

	}
	if (currentPanelId.equals(PivotWizardAnalysisPanel.SCREEN_ID)) {
	    return PivotWizardPostAnalysisFilterPanel.SCREEN_ID;

	}
	if (currentPanelId.equals(PivotWizardPostAnalysisFilterPanel.SCREEN_ID)) {
	    return PivotWizardPostAnalysisPivotPanel.SCREEN_ID;

	} else {
	    return null;
	}
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
	JRadioButton selectedOption = (JRadioButton) e.getItem();
	this.conditionalScreen = (selectedOption.getName().toString() == PivotWizardFirstPanel.CONDITIONAL_ANALYSIS_PER_SUBJECT_PER_CONDITION);
	wizardPanel.getNextButton().setEnabled((selectedOption.isSelected()));
    }
}
